import {
  createScopedThreejs
} from './threejs-miniprogram'
import {
  registerGLTFLoader
} from './loaders/gltf-loader'
//


const info = wx.getSystemInfoSync()

export default function getBehavior() {
  return Behavior({
    // behaviors: [recogBehavior],
    data: {
      width: 1,
      height: 1,
      fps: 0,
      memory: 0,
      cpu: 0,
      cameraPosition: 0, // 0: 后置; 1: 前置

    },
    lifetimes:{ // 保存情况之一
      detached() {
        if(this.data.group_count > 0 || this.data.my_count > 0) { 
          this.setData({infer_log: 'here before [saveRecord]'})
          this.saveRecord(1)
          this.setData({infer_log: 'here before [clearData]'})
          this.clearData()
        }
        else {}
        console.log("动作计数detached")
      }
    },
    methods: {
      onReady() {
        wx.createSelectorQuery()
          .select('#webgl')
          .node()
          .exec(res => {
            this.canvas = res[0].node

            const info = wx.getSystemInfoSync()
            const pixelRatio = info.pixelRatio
            const calcSize = (width, height) => {
              console.log(`canvas size: width = ${width} , height = ${height}`)
              this.canvas.width = width * pixelRatio
              this.canvas.height = height * pixelRatio
              this.setData({
                width,
                height,
              })
            }
            calcSize(info.windowWidth, info.windowHeight)

            this.initVK()
          })
      },
      onUnload() {
        if (this._texture) {
          this._texture.dispose()
          this._texture = null
        }
        if (this.renderer) {
          this.renderer.dispose()
          this.renderer = null
        }
        if (this.scene) {
          this.scene.dispose()
          this.scene = null
        }
        if (this.camera) this.camera = null
        if (this.model) this.model = null
        if (this._insertModel) this._insertModel = null
        if (this._insertModels) this._insertModels = null
        if (this.planeBox) this.planeBox = null
        if (this.mixers) {
          this.mixers.forEach(mixer => mixer.uncacheRoot(mixer.getRoot()))
          this.mixers = null
        }
        if (this.clock) this.clock = null

        if (this.THREE) this.THREE = null
        if (this._tempTexture && this._tempTexture.gl) {
          this._tempTexture.gl.deleteTexture(this._tempTexture)
          this._tempTexture = null
        }
        if (this._fb && this._fb.gl) {
          this._fb.gl.deleteFramebuffer(this._fb)
          this._fb = null
        }
        if (this._program && this._program.gl) {
          this._program.gl.deleteProgram(this._program)
          this._program = null
        }
        if (this.canvas) this.canvas = null
        if (this.gl) this.gl = null
        if (this.session) this.session = null
        if (this.anchor2DList) this.anchor2DList = []
      },
      initVK() {
        // 初始化 threejs
        this.initTHREE()

        // 自定义初始化
        if (this.init) this.init()

        console.log('this.gl', this.gl)
        
        //const test_session = wx.createV
        const session = this.session = wx.createVKSession({
          track: {
            plane: {mode: 3},
            body: {mode: 1}
          },
          gl: this.gl,
          version: 'v2',
        })

        session.start(err => {
          if (err) return console.error('VK error: ', err)

          console.log('@@@@@@@@ VKSession.version', session.version)

          const canvas = this.canvas
          const calcSize = (width, height, pixelRatio) => {
            console.log(`canvas size: width = ${width} , height = ${height}`)
            this.canvas.width = width * pixelRatio
            this.canvas.height = height * pixelRatio
            this.setData({
              width,
              height,
            })
          }
          // here th canvas size set
          session.on('resize', () => {
            const info = wx.getSystemInfoSync()
            calcSize(info.windowWidth, info.windowHeight, info.pixelRatio)
          })

          session.on('addAnchors', anchors => {
            this.data.anchor2DList = anchors.map(anchor => ({
              points: anchor.points,
              origin: anchor.origin,
              size: anchor.size
            }))
          })

          session.on('updateAnchors', anchors => { // core 
            this.data.anchor2DList = []
            this.data.anchor2DList = this.data.anchor2DList.concat(anchors.map(anchor => ({
              points: anchor.points,
              origin: anchor.origin,
              size: anchor.size
            })))

            console.log("显示data")
            console.log(this.data)
          
            // add this
            this.setData({infer_log: 'to post-process'})

            const anchor2DList = this.data.anchor2DList
            if (anchor2DList && anchor2DList.length > 0){
              this.postProcess(anchor2DList)
              
              // judge pose
              var pose = 0
              if (this.pushDownJudge()) {
                pose = 1
                this.setData({squat_flag: true})
              }
              else if (this.pushUpJudge()) {
                pose = 0
              }
              else { // important
                pose = 2
              }

              if (this.data.last_pose == 2 && pose == 0 && this.data.squat_flag) { 
                this.setData({squat_flag: false})
                
                var temp_count = this.data.my_count + 1
                
                // group count
                if(temp_count == this.data.max_count) {
                  var temp_group = this.data.group_count + 1
                  this.setData({group_count: temp_group})
                  this.setData({my_count: 1})
                }
                else {
                  this.setData({my_count: temp_count})
                }

                // this.setData({my_count: temp})
              }
              this.setData({last_pose: pose})

              // update display
              if(this.data.last_count != this.data.my_count)
              {
                this.setData({
                  percentage: this.data.my_count / this.data.max_count * 100,
                  last_count: this.data.my_count
                })
              }
            }

          })

          session.on('removeAnchors', anchors => {
            this.data.anchor2DList = []
          })

          //限制调用帧率
          let fps = 30
          let fpsInterval = 1000 / fps
          let last = Date.now()

          // 逐帧渲染
          const onFrame = timestamp => {
            let now = Date.now()
            const mill = now - last
            // 经过了足够的时间
            if (mill > fpsInterval) {
              last = now - (mill % fpsInterval); //校正当前时间
              const frame = session.getVKFrame(canvas.width, canvas.height)
              if (frame) { 
                //add this 
                this.setData({infer_log: 'to renderer'})

                this.render(frame)
              }
            }
            session.requestAnimationFrame(onFrame)
          }
          session.requestAnimationFrame(onFrame)
        })
      },
      initTHREE() {
        const THREE = this.THREE = createScopedThreejs(this.canvas)
        registerGLTFLoader(THREE)

        // 相机
        this.camera = new THREE.Camera()

        // 场景
        const scene = this.scene = new THREE.Scene()

        // 光源
        const light1 = new THREE.HemisphereLight(0xffffff, 0x444444) // 半球光
        light1.position.set(0, 0.2, 0)
        scene.add(light1)
        const light2 = new THREE.DirectionalLight(0xffffff) // 平行光
        light2.position.set(0, 0.2, 0.1)
        scene.add(light2)

        // 渲染层
        const renderer = this.renderer = new THREE.WebGLRenderer({
          antialias: true,
          alpha: true
        })
        renderer.gammaOutput = true
        renderer.gammaFactor = 2.2
      },
    },
  })
}